<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/23
 * Time: 14:21
 */
namespace app\wechat\common\logic;

use think\Db;

class User{

    //校验用户是否关注
    public function validIssub($user){

        $param = [
            'key' => 'liquan',
            'openid' => $user['openid'],
        ];
        $users = \wechat\util\User::detail($param);
        if(isset($users['subscribe'])){
            if($users['subscribe'] == 1){
                $flag = $this->updateUser($users);
                return ['code'=>1,'data'=>['issub'=>1]];
            }else{
                $find = ['unionid'=>$user['unionid']];
                $flag = Db::name('mect_user')->where($find)->update(['issub'=>2]);
                return ['code'=>1,'data'=>['issub'=>2]];
            }
        }else{
            return ['code'=>0,'msg'=>[]];
        }
    }

    //更新用户微信资料
    public function updateUser($users){

        $find = ['unionid'=>$users['unionid']];
        $save = [
            'issub' => 1,
            'headpic' => $users['headpic'],
            'nickname' => $users['nickname'],
            'sex' => $users['sex'],
            'province' => $users['province'],
            'city' => $users['city'],
            'deltime' => NOW_TIME,
        ];
        return Db::name('mect_user')->where($find)->strict(false)->update($save);
    }
}